package com.zoer.hibdao;

// default package

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;

import com.zoer.domain.DrugType;

/**
 * A data access object (DAO) providing persistence and search support for
 * DrugType entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see .DrugType
 * @author MyEclipse Persistence Tools
 */

public class DrugTypeDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(DrugTypeDAO.class);
	// property constants
	public static final String TYPENAME = "typename";

	public void save(DrugType transientInstance) {
		log.debug("saving DrugType instance");
		try {
			Session s = getSession();
			s.beginTransaction();
			s.save(transientInstance);
			s.getTransaction().commit();
			s.close();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(DrugType persistentInstance) {
		log.debug("deleting DrugType instance");
		try {
			Session s = getSession();
			s.beginTransaction();
			s.delete(persistentInstance);
			s.getTransaction().commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public DrugType findById(java.lang.String id) {
		log.debug("getting DrugType instance with id: " + id);
		try {
			DrugType instance = (DrugType) getSession().get("com.zoer.domain.DrugType", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	@SuppressWarnings("unchecked")
	public List<DrugType> findByExample(DrugType instance) {
		log.debug("finding DrugType instance by example");
		try {
			Session s = getSession();
			Criteria c = s.createCriteria("com.zoer.domain.DrugType").add(
					Example.create(instance));
			List<DrugType> results = c.list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
	@SuppressWarnings("unchecked")
	public List<DrugType> findByProperty(String propertyName, Object value) {
		log.debug("finding DrugType instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from DrugType as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<DrugType> findByTypename(Object typename) {
		return findByProperty(TYPENAME, typename);
	}

	@SuppressWarnings("unchecked")
	public List<DrugType> findAll() {
		log.debug("finding all DrugType instances");
		try {
			String queryString = "from DrugType";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public DrugType merge(DrugType detachedInstance) {
		log.debug("merging DrugType instance");
		try {
			DrugType result = (DrugType) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(DrugType instance) {
		log.debug("attaching dirty DrugType instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(DrugType instance) {
		log.debug("attaching clean DrugType instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}